import prompt from '@system.prompt';
import router from '@system.router';
import http from '@ohos.net.http';
import featureAbility from '@ohos.ability.featureAbility'
import preferences from '@ohos.data.preferences';
// 获取context并调用相关方法
let context = featureAbility.getContext();
//轻量级数据库
let storagePromise = preferences.getPreferences(context, 'mystore');
export default {
    data: {
        title: "",
        password:"123",
        name:"YZJ",
        result:{

        }
    },
    onInit() {

    },
    onAccount(e){
        this.name=e.value
    },
    onPassword(e){
        this.password= e.value
    },
    onLogin(){
        let httpRequest = http.createHttp();
        httpRequest.request(
            "https://www.xdsbty.cn/login",
            {
                method:http.RequestMethod.POST,
                header: {
                    'Content-Type': 'application/json'
                },
                extraData:{
                    "Name":this.name,
                    "Password":this.password,
                },
                readTimeout: 10000,
                connectTimeout: 10000,

            }, (err, data) => {
            if (!err) {
                this.result = JSON.parse(data.result);
                if(this.result.isSuccess==true){
                    router.push({
                        uri:"pages/home/home"
                    })
                }
                else{
                    prompt.showToast({
                        message:this.result.message
                    })
                }

                console.info("YZJ--- "+JSON.stringify(data.result))
                console.info("YZJ---"+this.result.isSuccess)
                //---存储token到缓存
                storagePromise.then((preferences) => {
                    let putPromise = preferences.put('token', this.result.token);
                    preferences.flush();
                    putPromise.then(() => {
                        console.info("xxx---轻量级存储token成功");
                    }).catch((err) => {
                        console.info("xxx---轻量级存储token失败,data:" + err);
                    })
                }).catch((err) => {
                    console.info("xxx---获取preference失败："+err);
                })
                //----
            } else {
                console.info('YZJ--- error:' + JSON.stringify(err));
                let error = JSON.parse(err)
                prompt.showToast({
                    message:error.message
                })
            }
        })
    },
    onRegisterpage(){
        router.push({
            uri:"pages/register/register"
        })
    }

}
